package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderService;
import com.itheima.health.vo.OrderSubmitParam;
import com.itheima.health.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import okhttp3.internal.Internal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

/**
 * 预约ServiceImpl
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetMealDao setMealDao;

    @Override
    public Result order(OrderSubmitParam orderSubmitParam) {
        Setmeal setmeal=setMealDao.selectById(orderSubmitParam.getSetMealId());
        if(orderSubmitParam.getName() == null || orderSubmitParam.getName() == ""){
            return new Result(false,"体检人不能为空！");
        }
        if(setmeal.getSex().equals("0")||setmeal.getSex().equals(orderSubmitParam.getSex())){
            //检查预约当天是否能预约
            OrderSetting orders = orderSettingDao.selectByDate(orderSubmitParam.getOrderDate());
            //如果选择的日期没有则预约失败
            if (orders == null) {
                return new Result(false,MessageConst.SELECTED_DATE_CANNOT_ORDER);
            }
            //检查可预约人数和已经预约的人数
            Integer number=orderSettingDao.getNumber(orderSubmitParam.getOrderDate());
            Integer reservation=orderSettingDao.getReservation(orderSubmitParam.getOrderDate());
            //如果可预约人数大于了已经预约的人数则不可预约
            if (number<=reservation){
                return new Result(false,MessageConst.ORDER_FULL);
            }
            //检查一下是否有会员，根据手机号查询
            Member member= memberDao.findByTelephone(orderSubmitParam.getTelephone());
            //如果是会员则获取id来进行查询当天是否预约
            if (member!=null){
                Integer memberId=member.getId();
                Date settingtime=orderSubmitParam.getOrderDate();
                Integer count=orderDao.findBysetcount(memberId,settingtime);
                System.out.println(count);
                //如果预约则不可进行预约
                if (count!=0){
                    return new Result(false,MessageConst.HAS_ORDERED);
                }
            }else{
                member = new Member();
                member.setName(orderSubmitParam.getName());
                member.setPhoneNumber(orderSubmitParam.getTelephone());
                member.setSex(orderSubmitParam.getSex());
                member.setIdCard(orderSubmitParam.getIdCard());
                member.setRegTime(new Date());
                memberDao.insert(member);
            }
            Order order= new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderSubmitParam.getOrderDate());
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setSetmealId(orderSubmitParam.getSetMealId());
            orderDao.insert(order);
            System.out.println(order);
            Integer reservations=orderDao.getReservation(orderSubmitParam.getOrderDate());
            orderSettingDao.updatereservation(orderSubmitParam.getOrderDate(),reservations);
            return new Result(true,MessageConst.ORDER_SUCCESS,order);
        }else {
            return new Result(false,"体检套餐与体检人性别不匹配");
        }

    }
/*
订单数据展示
 */
    @Override
    public OrderVO SelectOrdersById(String id) {
        //新建VO对象 返回           用户名称 ，套餐名称，时间信息，预约方式
        OrderVO orderVO = new OrderVO();
        //根据订单id--->order查询出 用户id，套餐id，时间信息，预约方式
        Order order = orderDao.getByOrderId(id);
        Integer memberId = order.getMemberId();//拿到用户id
        Integer setmealId = order.getSetmealId();//拿到套餐id
        Date orderDate = order.getOrderDate();//拿到时间信息
        //日期转换
        Instant instant = orderDate.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        //把时间信息，预约方式，用户名字，套餐名字封装进ordervo
        orderVO.setOrderDate(localDate.toString());
        orderVO.setOrderType(order.getOrderType());
        String memberName = memberDao.getByMemberId(memberId);
        log.info("-----------------------"+memberName);
        orderVO.setMember(memberName);
        orderVO.setSetmeal(memberDao.getBySetmealId(setmealId));
        return orderVO;//返回vo
    }



}
